import { Store } from 'iflux2'
import OrderActor from './actor/order-actor.js';
import MsgActor from './actor/msg-actor.js'
import RefundActor from './actor/refund-actor.js'
import LogActor from './actor/log-actor.js'
import { fromJS } from 'immutable'
import webapi from './webapi.js';
import { orderTypes, msgTypes, refundTypes, logTypes } from './constant/actionTypes.js'
import { message } from 'antd';
export default class AppStore extends Store {
    bindActor() {
        return [
            new OrderActor,
            new MsgActor,
            new RefundActor,
            new LogActor,
        ]
    }

    constructor(props){
        super(props)
    }
    /**
     * 获取售后订单详情
     * @param {*string } rid 服务单编号 
     */
    fetchOrder = (rid) => {
        return webapi.fetchOrder(rid)
                .then(order => {
                    this.dispatch(orderTypes.SET_ORDER,fromJS(order.apply));
                    this.dispatch(logTypes.SET_ORDER_LOG,fromJS(order.applyRecordList))
                    this.dispatch(logTypes.SET_REFUND_LOG,fromJS(order.refundReceipt || {}))
                    this.dispatch(logTypes.SET_RETURNED_LOG,fromJS(order.returnedReceipt || {}))
                    this.dispatch(orderTypes.SET_USER_LOGO,order.userLogo)
                    return order;
                })
                .catch(error=>{
                    message.error(error.message || '获取售后订单详细信息错误！')
                    return Promise.reject(error);

                })
    }
    /**
     * 审核售后订单
     * @param {boolean} checkout 
     */
    auditOrder = (checkout) => {
        const order = this.state().get('order');

        return webapi.auditAfterSaleOrder(order.get('applyId'),checkout)
                    .then((result)=>{
                        //审核通过，状态变成2，；审核不通过状态为6；
                        //this.dispatch(orderTypes.CHANGE_DATA,checkout ? fromJS({state:2}) : fromJS({state:6}));
                        this.dispatch(orderTypes.CHANGE_DATA,fromJS(result.apply));
                        this.dispatch(logTypes.SET_ORDER_LOG,fromJS(result.applyRecordList))
                        return result;
                    })
                    .catch((error)=>{
                        return Promise.reject(error);
                    })
    }
    /**
     * 取消售后订单
     */
    cancelOrder = () => {
        const order = this.state().get('order');

        return webapi.cancelAfterSaleOrder(order.get('applyId'))
                    .then((result) => {
                         //申请单直接关闭；
                        //this.dispatch(orderTypes.CHANGE_DATA,fromJS({state:7}));
                        this.dispatch(orderTypes.CHANGE_DATA,fromJS(result.apply));
                        this.dispatch(logTypes.SET_ORDER_LOG,fromJS(result.applyRecordList))
                        return result;
                    })
                    .catch(error=>{
                        return Promise.reject(error);
                    })
    }
    /**
     * 签收退货
     */
    receivedGoods = () => {
        const order = this.state().get('order');
        
        return webapi.receivedGoods(order.get('applyId'))
                    .then((result)=>{
                        //签收退货
                        //this.dispatch(orderTypes.CHANGE_DATA,fromJS({state:3}));
                        this.dispatch(orderTypes.CHANGE_DATA,fromJS(result.apply));
                        this.dispatch(logTypes.SET_ORDER_LOG,fromJS(result.applyRecordList))
                        this.dispatch(logTypes.SET_RETURNED_LOG,fromJS(result.returnedReceipt));
                        return result;
                    })
                    .catch((error)=>{
                        return Promise.reject(error);
                    })
    }
    /**
     *  查询可退款金额，退款方式，订单信息 
     */
    fetchRefundOrderInfo = () => {
        const order = this.state().get('order');
        return webapi.fetchRefundOrderInfo(order.get('applyId'))
                    .then((info) => {
                        this.dispatch(refundTypes.SET_REFUND_ORDER,fromJS(info));
                        return info;
                    })
                    .catch((error)=>{
                        message.error(error.message || '查询订单退款金额失败！')
                        return Promise.reject(error);
                    })
    }
    /**
     * 确认退款
     */
    confirmRefund = (refund) => {
        const order = this.state().get('order');

        return webapi.confirmRefund({tid:order.get('tid'),applyId:order.get('applyId'),...refund})
                    .then((result) => {
                        //this.dispatch(orderTypes.CHANGE_DATA,fromJS({state:4,actualeRfund:refund.refundAmount}));
                        this.dispatch(orderTypes.CHANGE_DATA,fromJS(result.apply));
                        this.dispatch(logTypes.SET_ORDER_LOG,fromJS(result.applyRecordList))
                        this.dispatch(logTypes.SET_REFUND_LOG,fromJS(result.refundReceipt || {}))
                        return result;
                    })
                    .catch((error)=>{
                        message.error(error.message || '订单退款失败！')
                        return Promise.reject(error);
                    })
    }
    /**
     * 查询卖家留言
     */
    fetchSellerMsg = () => {
        const order = this.state().get('order');
        
        return webapi.fetchSellerMsg(order.get('applyId'))
                    .then((messages)=>{
                        this.dispatch(msgTypes.RECEIVE_MESSAGES,fromJS(messages.map(msg=>msg.sellerMsg)))
                        return messages;
                    })
                    .catch((error)=>{
                        return Promise.reject(error);
                    })
    }
    /**
     * 添加卖家 留言
     * @param (string) msg 留言内容
     */
    appendSellerMsg = (msg) => {
        const order = this.state().get('order');
        const sellerMsg = {
            "tid":order.get('tid'),
            "applyId":order.get('applyId'),
            "message":msg
        }
        return webapi.appendSellerMsg(sellerMsg)
                    .then((result)=>{
                        this.dispatch(msgTypes.APPEND_MESSAGE,msg);
                        return result;
                    })
                    .catch((error)=>{
                        message.error(error.message || '获取售后订单详细信息错误！')
                        return Promise.reject(error);
                    })
    }
}